This article mainly introduces the ZipFile module in Python to use the detailed, ZipFile module is used to manipulate the zip file, the need for friends can refer to the
The zip file format is a common document compression standard, in the Ziplib module, the ZipFile class is used to manipulate the zip file, the following details:
Class
The Python zipfile module is used to compress and decompress the ZIP format code, ZipFile has two very important classes, ZipFile and Zipinfo, in the vast majority of cases, we just need to use the two class. ZipFile is the primary class that is used to create and read zip files, and Zipinfo is the information for each
A brief introduction of ZipFile moduleZipFile is used in Python to do zip format encoding compression and decompression, because it is a very common zip format, so this module use frequency is also relatively high,Here are some notes on how to use ZipFile. That is convenient for themselves and others.There are two very common classes in ZipFile, namely
This article mainly introduces how to use the zipfile module in Python. The zipfile module is used to operate zip files. If you need it, refer to the zip file format below, which is a common document compression standard, in the ziplib module, the ZipFile class is used to operate zip files. The following describes the operations:
Class
The Python programming language is an object-oriented programming language with powerful functions. It is easy to use and grasp. Today, let's take a look at one of the most important application modules and the related application methods of the Python ZipFile module.
Interpretation of Python compressed file basic application code example
Summary of common functions of the Python OS Module
How to find duplicate files in Python
Main functions of f
The Python zipfile module is used to compress and decompress zip code. zipfile contains two very important classes: ZipFile and ZipInfo. In most cases, we only need to use these two classes. ZipFile is the main class used to create and read zip files, while ZipInfo is the information of each file stored in the zip file
,
Format: Compressed package type, "Zip", "tar", "Bztar", "Gztar"
Root_dir: Folder path to compress (default current directory), which directory or file to package (that is, the source file)
Owner: User, default Current user
Group: Groups, default current group
Logger: Used to log logs, usually logging. Logger Object
Example Import Shutil shutil.make_archive ("Test_package_bak", "Zip", "test_package") #对文件夹下的test_package文件夹进行打包 Output results11, shuti
Solve the problem of Chinese path and garbled Characters During ZipFile decompression in java
When decompressing a jar or zip file in JAVA, you can use the built-in JDK APIs JarFile and ZipFile. when decompressing these two formats in windows, the following errors are often reported:
Exception in thread "main" java.lang.IllegalArgumentException: MALFORMEDat java.util.zip.ZipCoder.toString(ZipCoder.java:58)a
1) Simple application of ZipFileIf you are simply using Python for compression and decompression, the method is as followsImportZipfilef= ZipFile. ZipFile ('Filename.zip','W', ZipFile. zip_deflated) F.write ('1.txt') F.write ('2.doc') F.write ('3.zip') F.close () F.zipfile.zipfile ('Filename') F.extractall () f.close ()* ZipF
Python comes with a zipfile module for reading and writing zip files.def zip_dir (dirname,zipfilename): filelist = [] if Os.path.isfile (dirname): filelist.append (dirname) else: for root, dirs, files in Os.walk (dirname): For name in Files: filelist.append (Os.path.join (Root , name)) ZF = ZipFile. ZipFile
Online comments excerpt, you can poke the Web site, this is also the translation of foreign sites related comments:Class Zipinputstream reads a sequence of zip files (simply read out how many files are compressed by this zip file), and class ZipFile uses an embedded random file access mechanism to read the contents of the file, so you do not have to read the sequence of zip files sequentially.Another fundamental difference between Zipinputstream and
Python zipfile Module
#! /Usr/bin/env pythonimport zipfileimport OS # create a compress file for/etcz = zipfile. zipFile ('/root/etc.zip', 'w') for root, dirs, files in OS. walk ('/etc'): for file in files: z. write (OS. path. join (root, file) z. close () # check the name list of the compressed file 'etc.zip 'z = zipfile
When unpacking a jar file or zip file in Java, you can use the built-in Api:jarfile and ZipFile of the JDK to extract the 2 format files under Windows, often reporting the following error:Exception in thread "main" Java.lang.IllegalArgumentException:MALFORMEDat java.util.zip.ZipCoder.toString ( zipcoder.java:58) at Java.util.zip.ZipFile.getZipEntry (zipfile.java:531) at java.util.zip.zipfile.access$900 ( zipfile.java:56) at Java.util.zip.zipfile$1.nex
#!/usr/bin/env python#-*-coding:utf-8-*- fromZipFileImport*ImportZipFile#Unzip the zip filedefunzip (): Source_zip="C:\\update\\sw_servers_20120815.zip"Target_dir="c:\\update\\"Myzip=ZipFile (source_zip) myfilelist=myzip.namelist () forNameinchMyfilelist:f_handle=open (Target_dir+name,"WB") F_handle.write (myzip.read (name)) F_handle.close () myzip.close ()#add a file to an existing ZIP packagedefAddzip (): F= Zi
Python has the most abundant and powerful class libraries in the scripting language and is sufficient to support most daily applications. Python modules and C can work together and can be embedded into C or C ++ applications, therefore, the Python language can be used to provide script interfaces for applications.
Here we will record how to use zipfile. That is, it is convenient for you and others. Zipfile
The main problem is to solve the compression and decompression of empty folders in compressed directory.Functions for compressing folders:1# coding:utf-82 Import OS3 Import ZipFile4 5 def zipdir (dirtozip,savepath):6 ifNot Os.path.isdir (dirtozip):7Raise Exception,u"zipdir error,not a dir '% '". Format (dirtozip)8 9(savedir,_) =os.path.split (Savepath)Ten ifNot Os.path.isdir (savedir): One os.makedirs (Savedir) A -Ziplist = [] - the forRoot,dirs,filesinchOs.walk (dirtozip): -
When extracting jar files or zip files in Java, you can use the built-in Api:jarfile and ZipFile of the JDK to extract the 2 format files under Windows, often reporting the following error:Exception in thread "main" Java.lang.IllegalArgumentException:MALFORMEDat java.util.zip.ZipCoder.toString ( zipcoder.java:58) at Java.util.zip.ZipFile.getZipEntry (zipfile.java:531) at java.util.zip.zipfile.access$900 ( zipfile.java:56) at Java.util.zip.zipfile$1.ne
ZipFile. ZipFile (FILE,MODE,COMPRESS_TYPE,ALLOWZIP64) #创建一个ZipFile对象File:zip file nameMode: ' R ' reads the existing zip file, ' W ' new or overwrites the existing zip file, ' a ' appended to the existing ZIP documentCompress_type:zipfile. Zip_stored is not compressed by default, ZipFile. zip_deflated compressionALLOWZ
Use the java.util.zip. zipfile class to decompress the file.
You can decompress the zip, jar, and war files, but cannot decompress the rarfile.
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.util.Enumeration;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;public class UnZip {static final int BUFFER = 1024;public void unzip(Fil
Recently read the ZIP compression package in Python. To report a mistake.Python 2.6.6 (r266:84292, June, 14:18:47) [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on Linux2type "help", "copyright", "Credits" or "license" for more information.>>> import zipfile>>> ZipFile. ZipFile (' bla.apk ') Traceback (most recent call last): file "This should be considered a Python bu
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.